package org.example;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MiniApplicationContext {

    private Map<String, Object> beans;

    Map<String, BeanDefinition> beanDefinitions = new HashMap<>();
    public  void parse(String path) throws IOException, DocumentException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        SAXReader reader = new SAXReader();
        Resource resource = new ClassPathResource(path);

        InputStream is = resource.getInputStream();
        Document doc = reader.read(is);
        Element root = doc.getRootElement();
        //5、开始解析元素
        List<Element> es = root.elements();
        for (int i = 0; i < es.size(); i++) {
            Element my = es.get(i);
            String id = my.attributeValue("id");
            String className = my.attributeValue("class");

            Class<?> clazz = Class.forName(className);
// 获取无参数构造器
        //    Constructor<?> constructor = clazz.getConstructor();
// 使用构造器创建实例对象


      //      Object instance = constructor.newInstance();
       //     Field nameField = clazz.getDeclaredField("id");
         //   nameField.setAccessible(true);
        //    nameField.set(instance, my.element("constructor-arg").attributeValue("value"));
            BeanDefinition beanDefinition = new BeanDefinition(my.element("constructor-arg").attributeValue("value"));
            beanDefinitions.put(id, beanDefinition);
        }
    }



}
